#if !defined(__TIMER_ACTION_H__)
#define __TIMER_ACTION_H__

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#define ACTION_HANDLE_ALL 0xFF

#pragma pack(4)
typedef uint8_t (*timer_action_create_cb_t)(void *timer_action, uint8_t handle, uint8_t param);
// 动作回调函数类型定义
typedef uint8_t (*action_callback_t)(uint8_t handle, uint8_t param);

typedef struct
{
    const char *const name;      /* 参数名称 */
    const char *const desc;      /* 参数描述 */
    const char *const i18n;      /* 参数名称翻译 */
    const char *const desc_i18n; /* 参数描述翻译 */
} timer_action_handle_item_t;

typedef struct
{
    const char *const name; /* 参数名称 */
    const char *const i18n; /* 参数名称翻译 */
    uint16_t value;         /* 值 */
    uint16_t handles;       /* 绑定参数 */
} timer_action_param_item_t;

typedef struct
{
    const char *const name; // 动作名称
    action_callback_t cb;
    const timer_action_handle_item_t *const handles;
    const timer_action_param_item_t *const params;
    uint16_t handles_len;
    uint16_t params_len;
} timer_action_item_s, *timer_action_item_t;
#pragma pack()

#define TIMER_ACTION_SECTION(number) __attribute__((used, section(".timer_action." #number)))
#define TIMER_ACTION_BASE(NAME, Create, Handles, Params, LEVEL)                        \
    const TIMER_ACTION_SECTION(LEVEL) timer_action_item_s __timer_action_item_##NAME = \
        {                                                                              \
            .name = #NAME,                                                             \
            .cb = (action_callback_t)Create,                                           \
            .handles = Handles,                                                        \
            .handles_len = (uint8_t)(sizeof(Handles) / sizeof(Handles[0])),            \
            .params = Params,                                                          \
            .params_len = (uint8_t)(sizeof(Params) / sizeof(Params[0])),               \
    };
#define ADD_TIMER_ACTION(NAME, Create, Handles, Params) TIMER_ACTION_BASE(NAME, Create, Handles, Params, 0)

timer_action_item_t timer_action_find(char *name);
uint8_t timer_action_call(char *name, uint8_t param, uint8_t handle);
uint16_t timer_action_item_to_json(timer_action_item_t item, char *out, uint16_t size);
uint16_t timer_actions_to_json(char *out, uint16_t size);

#endif // __TIMER_ACTION_H__
